package com.getpebble.android.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import com.getpebble.android.PebbleApplication;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.core.util.PblPreferences;
import com.getpebble.android.common.core.util.PblPreferences.PrefKey;
import com.getpebble.android.common.model.PblDevice;
import com.getpebble.android.common.util.DiscoveryUtils;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class MigrationUtil {
    private static final PrefKey[] BOOL_PREFS_TO_MIGRATE = new PrefKey[]{PrefKey.ALWAYS_NOTIFY, PrefKey.ALLOW_THIRD_PARTY_NOTIFICATIONS, PrefKey.CALL_NOTIFICATIONS, PrefKey.CALENDAR_NOTIFICATIONS, PrefKey.ANALYTICS_OPTIN, PrefKey.DEVELOPER_CONNECTION, PrefKey.NAGGED_ABOUT_DISCONNECT, PrefKey.NAGGED_ABOUT_CONNECT_IS_DISCONNECT};
    private static final PrefKey[] STRING_PREFS_TO_MIGRATE = new PrefKey[]{PrefKey.MUSIC_TARGET};
    private static final String TAG = MigrationUtil.class.getSimpleName();
    private Context mContext;
    private PblDevice mLastConnectedDevice;
    private PblPreferences mNewPblPrefs;
    private SharedPreferences mOldSharedPrefs;

    public MigrationUtil(PebbleApplication application) {
        this.mOldSharedPrefs = application.getNativeSharedPreferences(application.getPackageName() + "_preferences", 0);
        this.mNewPblPrefs = new PblPreferences(application);
        this.mContext = application.getApplicationContext();
    }

    public boolean holoMigrationRequired() {
        return !this.mNewPblPrefs.getBooleanData(PrefKey.MIGRATED_TO_HOLO, false);
    }

    private boolean wasOnboardingCompleted() {
        try {
            String value = this.mOldSharedPrefs.getString("PREF_SETUP_STATE_SETUP_COMPLETE", null);
            if (value == null || Integer.parseInt(value.split(",")[0]) != 0) {
                return false;
            }
            return true;
        } catch (Exception e) {
            Trace.debug(TAG, "wasOnboardingCompleted", e);
            return false;
        }
    }

    private void migrateSharedPrefs() {
        Boolean val;
        Map<String, Boolean> boolPrefs = new HashMap();
        Map<String, String> stringPrefs = new HashMap();
        for (PrefKey prefKey : BOOL_PREFS_TO_MIGRATE) {
            String key = this.mNewPblPrefs.prefKeyToString(prefKey);
            if (this.mOldSharedPrefs.contains(key)) {
                try {
                    val = Boolean.valueOf(this.mOldSharedPrefs.getBoolean(key, false));
                    if (val != null) {
                        boolPrefs.put(key, val);
                    }
                } catch (ClassCastException e) {
                    Trace.debug(TAG, key, e);
                }
            }
        }
        for (PrefKey prefKey2 : STRING_PREFS_TO_MIGRATE) {
            key = this.mNewPblPrefs.prefKeyToString(prefKey2);
            if (this.mOldSharedPrefs.contains(key)) {
                try {
                    String val2 = this.mOldSharedPrefs.getString(key, null);
                    if (val2 != null) {
                        stringPrefs.put(key, val2);
                    }
                } catch (ClassCastException e2) {
                    Trace.debug(TAG, key, e2);
                }
            }
        }
        Editor newPrefsEditor = this.mNewPblPrefs.getSharedPreferences().edit();
        for (Entry<String, ?> entry : this.mOldSharedPrefs.getAll().entrySet()) {
            if (((String) entry.getKey()).startsWith(this.mNewPblPrefs.prefKeyToString(PrefKey.GMAIL_ACCOUNT_ENABLED_PREFIX))) {
                try {
                    val = (Boolean) entry.getValue();
                    if (val != null) {
                        newPrefsEditor.putBoolean((String) entry.getKey(), val.booleanValue());
                    }
                } catch (ClassCastException e22) {
                    Trace.debug(TAG, (String) entry.getKey(), e22);
                }
            } else if (((String) entry.getKey()).startsWith(this.mNewPblPrefs.prefKeyToString(PrefKey.GMAIL_ACCOUNT_UID_PREFIX))) {
                try {
                    Long val3 = (Long) entry.getValue();
                    if (val3 != null) {
                        newPrefsEditor.putLong((String) entry.getKey(), val3.longValue());
                    }
                } catch (ClassCastException e222) {
                    Trace.debug(TAG, (String) entry.getKey(), e222);
                }
            }
        }
        this.mOldSharedPrefs.edit().clear().commit();
        for (String key2 : boolPrefs.keySet()) {
            newPrefsEditor.putBoolean(key2, ((Boolean) boolPrefs.get(key2)).booleanValue());
        }
        for (String key22 : stringPrefs.keySet()) {
            newPrefsEditor.putString(key22, (String) stringPrefs.get(key22));
        }
        newPrefsEditor.commit();
    }

    public void doHoloMigration() {
        Trace.debug(TAG, "doHoloMigration()");
        Trace.debug(TAG, "deleting db pebble.db: " + this.mContext.deleteDatabase("pebble.db"));
        Trace.debug(TAG, "deleting db datalogging: " + this.mContext.deleteDatabase("datalogging"));
        clearDiskFiles();
        if (!this.mOldSharedPrefs.getBoolean("pref_key_dont_auto_connect", false)) {
            String lastConnectedDevice = this.mOldSharedPrefs.getString("last_connected_pebble", null);
            if (lastConnectedDevice != null) {
                String deviceName = DiscoveryUtils.getPebbleDisplayName(null, lastConnectedDevice, this.mContext);
                this.mLastConnectedDevice = new PblDevice(deviceName, lastConnectedDevice);
                Trace.debug(TAG, "Setting mLastConnectedDevice = " + deviceName + " (" + lastConnectedDevice + ")");
            }
        }
        boolean onboardingCompleted = wasOnboardingCompleted();
        Trace.debug(TAG, "onboardingCompleted = " + onboardingCompleted);
        if (onboardingCompleted) {
            this.mNewPblPrefs.setBooleanData(PrefKey.ONBOARDING_COMPLETED, true);
            this.mNewPblPrefs.setBooleanData(PrefKey.TUTORIAL_COMPLETED, true);
        }
        migrateSharedPrefs();
        Trace.debug(TAG, "Migration complete");
        this.mNewPblPrefs.setBooleanData(PrefKey.MIGRATED_TO_HOLO, true);
    }

    public PblDevice getLastConnectedDevice() {
        return this.mLastConnectedDevice;
    }

    private void clearDiskFiles() {
        deleteDir(this.mContext.getCacheDir());
        deleteDir(this.mContext.getExternalCacheDir());
    }

    private static boolean deleteDir(File dir) {
        Trace.debug(TAG, "deleteDir: " + dir);
        if (dir != null) {
            try {
                if (dir.isDirectory()) {
                    for (String child : dir.list()) {
                        deleteDir(new File(dir, child));
                    }
                }
                return dir.delete();
            } catch (Exception e) {
                Trace.debug(TAG, "deleteDir", e);
            }
        }
        return false;
    }
}
